/*Author:S.Genikhov
Creatin Date:
Last modified:
Reviewed by :                at
Short project description:
Generic Server, work with the STATLESS Clients. Identification
of each client performs by identification handler (idClient), wich will
be supplyed by server to appropriate functions*/

#ifndef __GENSERVER__
#define __GENSERVER__

#include <stdio.h>

/*In Bellow list the prototypes of Callback function are given:
NewClientCb -   determined the application behavior on creation
NewMesssageCb - application behaviour when message arrived
NewExeptionCb - application behaviour when exeption arrived
ClientDiscnCb - application behaviour when client is disconnet durring
                reading or writing data trancaction, "_notfinished is
                the quantity of bytes that not transfered*/

typedef void (*NewClientCb)  (int _idClient,void* _app);
typedef void (*NewMesssageCb)(int _idClient,void* _app, void*  _buf,  size_t _mesSize);
typedef void (*NewExeptionCb)(int _idClient,void* _app, void*  _buf,  size_t _mesSize);
typedef void (*ClientDiscnCb)(int _idClient,void* _app, size_t _notreaded, size_t _notwrited);

typedef struct GenericServer_t GenSv;
/*Returns pointer to server.
 * - determnide by Application - Client protocol.
 *Argument 2 void* _appdata - It is  data supplyed by applicatoin
 which can be used by Callback functions.
Arguments 3 and 4 - determined by Application - Client protocol and
used to evaluate the length of accepted messages from Clients.
Arguments 5-9 are the "CallBack" function has to be supplyed by user.
These function determined behaviour of server in different situations.
Argument _exptMessagSize is the constant size of OOB messages
determined by prococol. OOB messages sende to application
by NewExeptionCb  */
typedef size_t (*HeaderFunc)(void* _buffer);
GenSv*  GsvServerNew(int _port, void* _appdata,int _headersize,
        HeaderFunc,
        NewClientCb,NewMesssageCb,ClientDiscnCb,
        size_t _exptMessagSize,NewExeptionCb);

/*Turns On Sever.
Return 0  if o.k. and err_Gsv if fails*/
int    GsvServerRun(GenSv* _this);

/*Return 0  if o.k. and err_SV if fails*/
int    GsvServerWrite(GenSv* _this,int _idClient, void* _buf,size_t _bufsize);

/*Disconnect the Client with correcsponded idClient*/
void   GsvServerDrop(GenSv*  _this, int _idClient);

/*Stop the Server activity, (opposite to Run). Before to "Stop" the server activity
 competele the all current action of read/write*/
void   GsvServerStop(GenSv*  _this);

void   GsvServerDelete(GenSv* _this);

typedef enum err_Gsv
{
    GSV_SERVER_OK                           = 0,
    GSV_NEW_CLIENT_CONNECTION_PROBLEM       = 5,
    GSV_NEW_CLIENT_CREATION_PROBLEMS        = 10,
    GSV_NEW_CLIENT_INSERTION_PROBLEMS       =15

} err_Gsv;

#endif /*__GENSERVER__*/
